package marchsoft.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import javax.annotation.PostConstruct;

/**
 * @title MySpringMvcConfigurer
 * @description
 * @author Lenovo
 * @version 1.0
 * @create 2024-09-27 17:46
 */
//@Configuration
public class MySpringMvcConfigurer implements WebMvcConfigurer{

    @Autowired
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    //前端传的字符串时间处理
    @PostConstruct
    public void addConversionConfig() {
        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter.getWebBindingInitializer();
        if (initializer.getConversionService() != null) {
            GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
            //添加字符串转换为日期类型的字符串
            genericConversionService.addConverter(new StringToLocalDateTimeConverter());
        }
    }

    /*@Override
    public void addFormatters(FormatterRegistry registry) {
        *//**
         * 将HTTP请求映射到Controller方法的参数上后，Spring会自动进行类型转化。对于日期类型的参数，
         * Spring默认并没有配置如何将字符串转为日期类型。为了支持可按照指定格式转为日期类型，需要添加
         * 一个DateFormatter类：
         *//*
        registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
    }*/
}
